#include "rtda/jvm_stack.h"

namespace jvm {
namespace rtda {


void JvmStack::push(std::shared_ptr<Frame> frame) {
    if (_size >= _max_size) {
        throw std::runtime_error("java.lang.StackOverflowError");
    }
    _frames.push_back(frame);
    _top = frame;
    ++_size;
}

std::shared_ptr<Frame> JvmStack::pop() {
    if (_size == 0) {
        throw std::runtime_error("Jvm Stack underflow");
    }
    auto frame = _top;
    _frames.pop_back();
    if (_frames.empty()) {
        _top = nullptr;
    } else {
        _top = _frames.back();
    }
    --_size;
    return frame;
}

std::shared_ptr<Frame> JvmStack::top() const 
{
    if (_top == nullptr) {
        throw std::runtime_error("Jvm Stack is empty");
    } 
    return _top; 
}

} // namespace rtda
} // namespace jvm
